package net.ibizsys.eclipse.studio.xml;

import java.util.Collections;

import net.ibizsys.eclipse.studio.util.StringKit;

import org.w3c.dom.Node;

/**
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public abstract class IBizView extends IBizSearchableNode
{
	/**
	 * 代码名称
	 */
	public static final String ATTR_CODENAME = "CODENAME";
	/**
	 * 标题
	 */
	public static final String ATTR_TITLE = "TITLE";
	/**
	 * 视图类型
	 */
	public static final String ATTR_VIEWTYPE = "VIEWTYPE";

	/**
	 * 模块标识
	 */
	public static final String ATTR_PSAPPMODULEID = "PSAPPMODULEID";

	private String codeName;
	private String title;
	private String viewType;
	private String moduleId;

	private IBizControlList controls = new IBizControlList();

	@Override
	protected void initAttribute(String name, String value)
	{
		if (StringKit.IsEqual(ATTR_CODENAME, name, true))
		{
			this.codeName = value;
			return;
		}

		if (StringKit.IsEqual(ATTR_TITLE, name, true))
		{
			this.title = value;
			return;
		}

		if (StringKit.IsEqual(ATTR_VIEWTYPE, name, true))
		{
			this.viewType = value;
			return;
		}

		if (StringKit.IsEqual(ATTR_PSAPPMODULEID, name, true))
		{
			this.moduleId = value;
			return;
		}

		super.initAttribute(name, value);
	}

	@Override
	protected void initChildNode(Node node, IBizProject project)
	{
		if (StringKit.IsEqual(node.getNodeName(), IBizControlList.NODE_PSCONTROLS, true))
		{
			controls.init(node, project);
			return;
		}

		super.initChildNode(node, project);
	}

	public String getCodeName()
	{
		return codeName;
	}

	public String getTitle()
	{
		return title;
	}

	public String getViewType()
	{
		return viewType;
	}

	public String getModuleId()
	{
		return moduleId;
	}

	public IBizControlList getControls()
	{
		return controls;
	}

	@Override
	public String getText()
	{
		return getTitle();
	}

	@Override
	public String getDesc()
	{
		return getCodeName();
	}

	@Override
	public boolean match(String keyword)
	{
		if (StringKit.has(getTitle(), keyword) || StringKit.has(getCodeName(), keyword))
		{
			return true;
		}
		return false;
	}

	@Override
	public void sortChildren()
	{
		Collections.sort(controls, new XmlNodeTextComparator<XmlNode>());
	}
}
